package com.foreveross.proxyip.application.impl.monitor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.proxyip.application.monitor.SourceRecordApplication;
import com.foreveross.proxyip.entity.ExtractLogEntity;
import com.foreveross.proxyip.infra.PageData;
import com.foreveross.proxyip.monitor.domain.SourceRecord;
import com.foreveross.proxyip.monitor.vo.ExtractLogEntityVO;

@Transactional
@Service
public class SourceRecordApplicationImpl implements SourceRecordApplication{

	@Override
	public PageData searchByConditions(ExtractLogEntityVO c, int pageIndex,
			int pageLen) throws Exception {
		if(pageIndex<=0){
			pageIndex=PageData.DEFAULT_PAGEINDEX;
		}
		if(pageLen<=0){
			pageLen=PageData.DEFAULT_PAGELEN;
		}
		List<ExtractLogEntity> es=SourceRecord.searchByConditions(c, pageIndex, pageLen);
		long size=SourceRecord.searchSizeByConditions(c);
		es=es==null?new ArrayList():es;
		List<Object> vos=new ArrayList();
		for(ExtractLogEntity e:es){
			ExtractLogEntityVO vo=new ExtractLogEntityVO(
					e.getId(), e.getBeginTime(),e.getCreateTime(), 
					e.getEndTime(), e.getExpendTime(), e.getExtractMode(),
					e.getExtractOtherMessage(), e.getExtractNum(), e.getState());
			vos.add(vo);
		}
		PageData p=new PageData();
		p.setDataSize(size);
		p.setPageIndex(pageIndex);
		p.setPageLen(pageLen);
		p.setResult(vos);
		int pageCount=(int)size/pageLen+((int)size%pageLen>0?1:0);
		p.setPageCount(pageCount);
		return p;
	}

}
